// pages/play/play.js
import request from '../../utils/request.js';

Page({
  
  /**
   * 页面的初始数据
   */
  data: {
  play: false,
  musicId: '',
  result: [],
  type: '',
  songTime: '',
  song: [],
  stringminute: '',
  stringsecond: '',
  currentTimeText: '00' ,
  currentTimeTextmin: 0,
  progressPercent: ''
  },

  // 获取音乐的数据
  async getMusicListData(musicId) {
    let songListData = await request('/song/detail' , {ids: musicId});
    let songTime = songListData.songs[0].dt;
    // console.log('当前歌曲的时间为：' + songTime + '毫秒')
    const time = songTime / 60000;
    const minute = parseInt(time);
    const second = parseInt((songTime - (60000 * minute)) / 1000);
    let stringminute = minute;
    let stringsecond = second;
    if(stringsecond < 9 && stringsecond >= 0){
      stringsecond = "0" + second
    }
    if(stringsecond >= 60){
      stringsecond = second - 60
      stringminute = minute + 1
    }else{
      stringminute = minute
    }
    this.setData({
      song: songListData.songs[0],
      songTime,
      stringminute,
      stringsecond
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // options 接收query的参数
    // console.log(options)
    let musicId = options.musicId;
    // 获取音乐详情
    this.getMusicListData(musicId);
    // console.log(musicId);

    // 判断当前页面是否在播放
    // if(appInstance.globalData.isMusicPlay && appInstance.globalData,musicId === musicId) {
    //   this.setData({
    //     play: true
    //   })
    // }

    // this.backgroundAudioManager = wx.getBackgroundAudioManager();
    // this.backgroundAudioManager.onPlay(() => {
    //   this.changePlayState(true);
    //   appInstance.globalData.musicId = musicId;
    // });
    // this.backgroundAudioManager.onPause(() => {
    //   this.changePlayState(false);
    // });
    // this.backgroundAudioManager.onStop(() => {
    //   this.changePlayState(false);
    // });

    // 接受recommendsong传来的id
    let iji = this.getOpenerEventChannel();
    let result = [];
    iji.on("ikunone" , data => {
      result.push(data)
      console.log( "onIkun", data)
    }),
    iji.on("ikuntwo" , data => {
      result.push(data)
      console.log( "onIkun", data)
    })

    const ikun = this.getOpenerEventChannel();
    // 接收参数
    ikun.on('cxk', data => {
      result.push(data)
      console.log( "onIkunone", data)
    })
    this.setData({
      result,
      musicId
    })


    // 监听音乐实时播放进度
    let backgroundAudioManager = wx.getBackgroundAudioManager();
    backgroundAudioManager.onTimeUpdate(() => {
      let progressPercent; //进度条
      let songtime  = parseInt(this.data.songTime  / 100);
      let currentTime = backgroundAudioManager.currentTime;
      let currentTimeText = parseInt(currentTime);
      let currentTimeTextmin = 0;
      progressPercent = parseInt((currentTimeText / songtime) * 1000);
      // console.log(progressPercent);
      // console.log('当前播放时间：', currentTimeText);
      if(currentTimeText < 10) {
        currentTimeText = '0' + currentTimeText;
      }else if(currentTimeText >= 60){
        let aaa = currentTimeText - (60 * (currentTimeTextmin + 1));
        if(aaa < 10){
          currentTimeText = '0' + aaa
        }else if(aaa >= 10){
          currentTimeText = aaa
        };
        currentTimeTextmin += 1
      };
      if(currentTimeTextmin >= 0 && currentTimeTextmin < 10) {
        currentTimeTextmin = '0' + currentTimeTextmin
      };
      // console.log(typeof currentTimeTextmin)
      this.setData({
        currentTimeText,
        currentTimeTextmin,
        progressPercent
      });
    });
  },
  // 播放暂停按钮
  begin() {
    let play = !this.data.play;
    this.setData({
      play
    })
    let {musicId} = this.data;
    this.getMusicstate(play , musicId);
  },
  // 音乐的播放与暂停
  async getMusicstate(play , musicId) {
    let backgroundMusic = wx.getBackgroundAudioManager();
    if(play) {
      // 获取音乐的url
      let backgroundMusicListData = await request('/song/url' , {id: musicId})
      let backgroundMusicList = backgroundMusicListData.data[0].url;
      backgroundMusic.src = backgroundMusicList;
      backgroundMusic.title = this.data.song.name;
    }else{
      backgroundMusic.pause();
    }
  },

  // 切歌
  handOff(event) {
    let type = event.currentTarget.id;
    // console.log(type);
    const iji = this.getOpenerEventChannel();
    // 接收参数
    iji.on("ikun" , data => {
      result.push(data)
      // console.log( "onioIkun", data)
    }),
    // 发送参数
    iji.emit('reverseData' , {
      add02: type
    }),
    // 接收切歌后的musicId
    iji.on('changedMusicId' , data=> {
      // console.log('改变后的id为:' , data)
    })
    // 接收本地储存的切换歌曲的id
    let aaa = wx.getStorageSync('key')
    // 获取index
    let bbb = wx.getStorageSync('keyIndex')
    // console.log('play接收的为:' + aaa)
    this.getMusicListData(aaa);
    this.getMusicstate(true,aaa);
    this.setData({
      musicId: aaa
    })
  },

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})